package com.itlong.cloud.rabbitmq.consumer;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.itlong.cloud.POJO.PO.TimeTaskDeadMessageQueuePO;
import com.itlong.cloud.POJO.VO.property.AppUserGetDeviceAuthVO;
import com.itlong.cloud.constants.RabbitMQConstant;
import com.itlong.cloud.property.service.IPropertyDeviceAuthService;
import com.itlong.cloud.utils.words.StringHandlerUtil;
import com.rabbitmq.client.Channel;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;import org.slf4j.LoggerFactory;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.io.IOException;
import java.util.List;

/**
 * <desc>
 *      与旺龙一卡通对接-处理住户设备权限信息业务操作队列消费者
 * </desc>
 *
 * @createDate 2017-11-20
 */
@Component
public class HandleSmartCardPushAuthConsumer {
    private static final Logger LOG = LoggerFactory.getLogger(HandleSmartCardPushAuthConsumer.class);

    @Autowired
    IPropertyDeviceAuthService iPropertyDeviceAuthService;

    /**
     * <desc>
     *      用户设备权限业务操作队列消费者
     * </desc>
     * @param message 消息
     * @param channel
     * @return
     * @author zhangs
     * @createDate 2017/11/6
     */
    @RabbitListener(queues = RabbitMQConstant.RABBITMQ_DOCKWITHSMARTCARD_HANDLE_DEVICEAUTH_QUEUE)
    public void process(Message message, Channel channel) throws InterruptedException, IOException {
        byte[] body = message.getBody();
        //LOG.info("RabbitMQ_与旺龙一卡通对接-处理住户设备权限信息业务消费端:"+body);
        try {
            TimeTaskDeadMessageQueuePO timeTaskDeadMessageQueuePO = JSON.parseObject(new String(body), TimeTaskDeadMessageQueuePO.class);
            JSONObject dataJs = JSON.parseObject(timeTaskDeadMessageQueuePO.getMessageContent());
            JSONObject dataContent = dataJs.getJSONObject("data");
            if (StringUtils.isNotEmpty(dataContent.toJSONString())) {
                String userId = dataContent.getString("userId");
                //住户设备权限
                JSONArray appUserGetDeviceAuthVOArr = dataContent.getJSONArray("appUserGetDeviceAuthVOS");
                List<AppUserGetDeviceAuthVO> appUserGetDeviceAuthVOS = JSON.parseArray(appUserGetDeviceAuthVOArr.toJSONString(), AppUserGetDeviceAuthVO.class);
                String deviceUniqe = appUserGetDeviceAuthVOS.get(0).getDeviceUnique();
                String projectId = StringHandlerUtil.copySubStr(deviceUniqe, 0, 8);
                iPropertyDeviceAuthService.updateSmartCardUserDeviceAuth(projectId, userId, projectId, appUserGetDeviceAuthVOS);
            }
        } catch (Exception e) {
            channel.basicReject(message.getMessageProperties().getDeliveryTag(), false);
            LOG.error("【RabbitMQ_与旺龙一卡通对接-处理住户设备权限信息业务消费端】:解析异常",e);
        } finally {
            //手动确认消息消费成功。
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }
    }
}
